﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

/* ############################################################################################
 *           Matthijs Hilgers - Gerben Boot - Marcel Boelen - Joep van den Hoven
 *               1580499          1575754        1582702            1592146         
 * ############################################################################################
 * 
 *  Date     || Changed                                  || By              || Source
 *  ===========================================================================================
 *  28-03-12 || First version                            || Gerben          || new
 */  

namespace MultipleOutputNeuralNetwork {
    public partial class Form1 : Form {
        private Bitmap InputImage;
        private NeuralNetwork neuralNetwork;
        private Stopwatch sw = new Stopwatch();

        public Form1() {
            InitializeComponent();
            this.neuralNetwork = new NeuralNetwork(100*100, 7);
        }

        private void openImage() {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            oFD.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.PNG;)|*.BMP;*.JPG;*.JPEG;*.PNG;";
            oFD.FilterIndex = 1;
            oFD.RestoreDirectory = true;
            if (oFD.ShowDialog() == DialogResult.OK) {
                String CurrentFileName = Path.GetFullPath(oFD.FileName);
                InputImage = new Bitmap(CurrentFileName);
                InputPictureBox.Image = InputImage;
            }
        }

        private void InputPictureBox_Click(object sender, EventArgs e) {
            openImage();
            if (InputImage != null) {
                char c = neuralNetwork.getChar(InputImage);
                CharacterTextBox.Text = c + "";
            }
        }

        private void LoadSetButton_Click(object sender, EventArgs e) {
            sw.Reset();
            sw.Start();
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            //dlg.Description = "Select a folder";
            if (dlg.ShowDialog() == DialogResult.OK) {
                LoadSetButton.Enabled = false;
                //InfoTextBox.AppendText(dlg.SelectedPath);
                string[] fileEntries = Directory.GetFiles(dlg.SelectedPath);
                int iterations = 0;
                while (true) {
                    iterations++;
                    //learn all bitmaps
                    Boolean neutralNetworkLearnt = false;
                    foreach (string FileDirectory in fileEntries) {
                        try {
                            Bitmap b = new Bitmap(FileDirectory);
                            String s = Path.GetFileNameWithoutExtension(FileDirectory).ToUpper();
                            char c = s.Last();
                            if (neuralNetwork.learn(b, c))
                                neutralNetworkLearnt = true;
                        } catch { }
                    }

                    if (!neutralNetworkLearnt){
                        InfoTextBox.AppendText("Iterations: "+iterations + "\n");
                        break;
                    } else if (iterations > 1000) {
                        InfoTextBox.AppendText("max iterations passed: " + iterations +"\n");
                        break;
                    }
                }
                sw.Stop();
                InfoTextBox.AppendText("Time: " + sw.ElapsedMilliseconds + "\n");
                LoadSetButton.Enabled = true;
            }
        }
    }
}
